<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
    
    
    <!-- <el-form-item label="所属村庄id" prop="countryId" v-if="false">
      <el-input v-model="dataForm.countryId" placeholder="所属村庄id"></el-input>
    </el-form-item> -->
    <el-form-item label="所属村庄" prop="countryName">
      <!-- <el-input v-model="dataForm.countryName" placeholder="所属村庄"></el-input> -->
      <el-select
        v-model="dataForm.countryName"
        filterable
        remote
        reserve-keyword
        @change="setCountryId(dataForm.countryName)"
        placeholder="搜索村庄名称"
        :remote-method="remoteMethod"
        :loading="loading">
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="门牌号码" prop="doorNum">
      <el-input v-model="dataForm.doorNum" placeholder="门牌号码"></el-input>
    </el-form-item>
    <el-form-item label="业主名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="业主名称"></el-input>
    </el-form-item>
    <el-form-item label="手机号" prop="mobile">
      <el-input v-model="dataForm.mobile" placeholder="手机号"></el-input>
    </el-form-item>
    <el-form-item label="是否户主" prop="householder">
      <el-select v-model="dataForm.householder" placeholder="请选择">
        <el-option value=1 label="是"></el-option>
        <el-option value=0 label="否"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="人员便签" prop="contract">
      <el-tag
        :key="tag"
        v-for="tag in dynamicTags"
        closable
        :disable-transitions="false"
        @close="handleClose(tag)">
        {{tag}}
      </el-tag>
      <el-input
        class="input-new-tag"
        v-if="inputVisible"
        v-model="inputValue"
        ref="saveTagInput"
        size="small"
        @keyup.enter.native="handleInputConfirm"
        @blur="handleInputConfirm"
      >
      </el-input>
      <el-button v-else class="button-new-tag" size="small" @click="showInput">+ New Tag</el-button>
      <!-- <el-input v-model="dataForm.contract" placeholder="人员便签"></el-input> -->
    </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: '',
          countryId: '',
          province: '',
          city: '',
          county: '',
          countryName: '',
          name: '',
          address: '',
          mobile: '',
          householder: 1,
          label: '',
          doorNum: '',
          contract: ''
        },
        loading: false,
        sysUsers: [],
        options: [],
        dynamicTags: [],
        inputVisible: false,
        inputValue: '',
        countryId: '',
        dataRule: {
          name: [
            { required: true, message: '村庄名称不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '街道地址不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/resident/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data) {
                this.dataForm.id = data.resident.id
                this.dataForm.province = data.resident.province
                this.dataForm.city = data.resident.city
                this.dataForm.county = data.resident.county
                this.dataForm.name = data.resident.name
                this.dataForm.address = data.resident.address
                this.dataForm.countryId = data.resident.countryId
                this.dataForm.countryName = data.resident.countryName
                this.dataForm.mobile = data.resident.mobile
                this.dataForm.householder = data.resident.householder
                this.dataForm.doorNum = data.resident.doorNum
                this.dynamicTags = data.resident.label.split(',')
              }
            })
          }

          this.$http({
            url: this.$http.adornUrl(`/country/all`),
            method: 'get'
          }).then(({data}) => {
            if (data) {
              this.sysUsers = data.data.map(item => {
                return {value: `${item.id}`, label: `${item.province}${item.city}${item.county}:${item.name}`}
              })
              console.log(this.sysUsers)
            }
          })
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/resident/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'mobile': this.dataForm.mobile,
                'countryId': this.dataForm.countryId,
                'householder': this.dataForm.householder,
                'doorNum': this.dataForm.doorNum,
                'label': this.dynamicTags.join(','),
                'createAt': this.dataForm.createAt
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      remoteMethod (query) {
        if (query !== '') {
          this.loading = true
          setTimeout(() => {
            this.loading = false
            this.options = this.sysUsers.filter(item => {
              return item.label.search(query) > -1
            })
          }, 200)
        } else {
          this.options = []
        }
      },
      handleClose (tag) {
        this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1)
      },
      showInput () {
        this.inputVisible = true
        this.$nextTick(_ => {
          this.$refs.saveTagInput.$refs.input.focus()
        })
      },
      handleInputConfirm () {
        let inputValue = this.inputValue
        if (inputValue) {
          this.dynamicTags.push(inputValue)
        }
        this.inputVisible = false
        this.inputValue = ''
      },
      setCountryId (countryId) {
        console.log('countryId=' + countryId)
        this.dataForm.countryId = countryId
      }
    }
  }
</script>
